package com.tools.cloud.payment.modules.payment.controller;


import com.tools.cloud.api.model.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.tools.cloud.payment.modules.payment.service.IPaymentService;
import com.tools.cloud.payment.modules.payment.model.Payment;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuyongjiang
 * @since 2021-11-18
 */
@Controller
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
    @Autowired
    private IPaymentService service;

    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "/selectOne", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Payment> selectOne(@RequestBody Payment param) {
        CommonResult<Payment> result = new CommonResult<Payment>();
        try {
            QueryWrapper<Payment> paramWrapper = new QueryWrapper<>(param);
            Payment getOne = service.getOne(paramWrapper);

            result = CommonResult.getSuccessObj("查询成功！");
            result.setErrInfo("端口号：" + port);
            result.setObject(getOne);
            return result;
        } catch(Exception e) {
          log.error("", e);
          return CommonResult.getErrObj("查询异常！");
        }
    }

    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    @ResponseBody
    public List<Payment> selectList(Payment param) {
        log.info("aaaaaaaaaa");
        QueryWrapper<Payment> paramWrapper = new QueryWrapper<>(param);
        return service.list(paramWrapper);
//        return Arrays.asList(new Payment(1l, "wqeqweqwe"));
    }

    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    @ResponseBody
    public Integer updateById(Payment param) {
        boolean update = service.updateById(param);
        return update ? 1 : 0;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Integer insert(Payment param) {
        boolean insert = service.save(param);
        return insert ? 1 : 0;
    }

    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    @ResponseBody
    public Boolean insertBatch(List<Payment> list) {
        return service.saveBatch(list);
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ResponseBody
    public Boolean deleteById(Payment param) {
        QueryWrapper<Payment> paramWrapper = new QueryWrapper<>(param);
        return service.remove(paramWrapper);
    }
}

